// <license>
//  Exception4Net is exception handler framework for dot net.
//  Copyright (C) 2002  sijingnan
//  
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2
//  of the License, or (at your option) any later version.
//  
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// 
// </license>
namespace Exception4Net {
	using System;
	using System.Resources;
	
	
	/// <summary>
	/// This is the base class for all application generated exceptions.
	/// Define common functions.
	/// </summary>
	/// <remarks>
	/// 	created by - sijingnan
	/// 	created on - 2002-9-11 15:45:04
	/// </remarks>
	public class AppException : System.Exception {
		private String localizedMessage = null;
		
		/// <summary>
		/// Default constructor - initializes all fields to default values
		/// </summary>
		public AppException():base() {
		}
		
		public AppException(String message) : base(message){
			
		}
		
		public AppException(String message,System.Exception exception) : base(message,exception){
			
		}

		///<summary>
		/// Represent the localized message of this exception.
		/// <remarks>
		/// localized message is used to show information to the end user.
		/// if localized message is not exist,then return the message of this exception
		/// </remarks>
		/// </summary>		
		public String LocalizedMessage{
			set{
				this.localizedMessage = value;		
			}
			
			get {
				String message = GetLocalizedMessage(this);

				return (message == null) ? this.Message : message;
			}
		}
		
		///<summary>
		/// return the localized message of the parameter appException
		/// <param name="appException">
		/// the AppException
		/// </param>
		/// <remarks>
		/// </remarks>
		/// </summary>
		private String GetLocalizedMessage(AppException appException){
			if (appException.localizedMessage != null){
				return appException.localizedMessage;
			}
			
			//check inner class first
			System.Exception inner = appException.InnerException;
			
			while(inner != null){
				if (inner is AppException){
					AppException app = (AppException)inner;
					
					//GetLocalizedMessage will check the exceptions left in the chain
					//so no need to loop here.
					String msg = GetLocalizedMessage(app);

					if (msg != null ) return msg;
				}
				
				inner = inner.InnerException;
			}
			
			//Check class type,the localizedMessage of type will be stored in the resource files.
			Type type = appException.GetType();

			ResourceManager rm = ExceptionHelper.Instance.DefaultResourceManager;

			if (rm == null)
				return null;

			String message = rm.GetString(type.FullName);

			while ((message == null) && (type.BaseType != null)) {
				type = type.BaseType;

				message = rm.GetString(type.FullName);
			}

			return message;
		}
	}
}
